package com.phamkhanh.bo;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.phamkhanh.dao.GpsDAO;
import com.phamkhanh.domain.Gps;
import com.phamkhanh.domain.User;

public class GpsBOImpl implements GpsBO {
	private GpsDAO gpsDAO;
	
	@Autowired
	public GpsBOImpl(GpsDAO gpsDAO) {
		super();
		this.gpsDAO = gpsDAO;
	}


	@Override
	@Transactional
	public void save(Gps gps, User user) {
		gps.setUser(user);
		gpsDAO.save(gps);
	}


	@Override
	@Transactional(readOnly=true)
	public List<Gps> findGpsByUser(User user) {
		Criteria criteria = gpsDAO.getSession().createCriteria(Gps.class);
		criteria.add(Restrictions.eq("user", user))
				.addOrder(Order.desc("dateUpdate"))
				.setMaxResults(5);
		return criteria.list();
	}
}
